package pub.kilo.thirdpush;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

import cn.jiguang.jmlinksdk.api.JMLinkAPI;
import cn.jiguang.jmlinksdk.api.JMLinkResponse;
import cn.jiguang.jmlinksdk.api.JMLinkResponseObj;

public class SecondActivity extends AppCompatActivity {
    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        init();
    }

    private void init() {
        Uri uri = getIntent().getData();
        Log.e(TAG, "data = " + uri);
        JMLinkAPI.getInstance().register(new JMLinkResponse() {
            @Override
            public void response(JMLinkResponseObj obj) {
                String invCode = JMLinkAPI.getInstance().getParam("u_id");
                Log.e(TAG, "replay = " + obj.paramMap + " " + obj.uri + " " + obj.source + " " + obj.type);
                Log.e(TAG, "invCode = " + invCode);
                //获取参数，进行后续工作
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                for (Map.Entry<String, String> param : obj.paramMap.entrySet()) {
                    intent.putExtra(param.getKey(), param.getValue());
                }
                startActivity(intent);
                finish();
            }
        });
        JMLinkAPI.getInstance().routerV2(uri);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Uri uri = intent.getData();
        Log.e(TAG, "data = " + uri);
        JMLinkAPI.getInstance().routerV2(uri);
    }
}
